/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Aptech.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import Aptech.DAL.MgBuilts;
import Aptech.DAL.MgDocument;
import Aptech.DAL.MgLands;
import Aptech.DALInterface.BuiltsInterface;
import Aptech.DALInterface.DocumentInterface;
import Aptech.DALInterface.LandsInterface;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.CatalogyDocument;
import HibernateMappingPoJo.Documnets;
import HibernateMappingPoJo.Lands;

/**
 * 
 * @author nkhoan
 */
public class SV_Documents {
	private DocumentInterface mgDoc;
	private BuiltsInterface mgBuilt;
	private LandsInterface mgLand;

	public SV_Documents() {
		mgDoc = new MgDocument();
		mgBuilt = new MgBuilts();
		mgLand = new MgLands();
	}

	public void addDoc(Documnets doc) {
		mgDoc.addDoc(doc);
	}

	public void saveFile(String fileName, String filePath, byte[] fileData)
			throws FileNotFoundException, IOException {
		mgDoc.saveFile(fileName, filePath, fileData);
	}

	public CatalogyDocument[] getAllCatelogyDoc() {

		List list = mgDoc.getAllCatelogyDoc();

		CatalogyDocument[] listCatelogy = new CatalogyDocument[list.size()];
		if (list != null) {
			Iterator iter = list.iterator();
			int i = 0;
			while (iter.hasNext()) {
				CatalogyDocument tptd = (CatalogyDocument) iter.next();
				listCatelogy[i] = tptd;
				i++;
			}
		}
		return listCatelogy;
	}

	public Documnets[] getAllDocument() {
		List list = mgDoc.getAllDocument();
		String idType = null;

		Documnets[] listDoc = new Documnets[list.size()];
		if (list != null) {
			Iterator iter = list.iterator();
			int i = 0;
			while (iter.hasNext()) {
				Documnets tptd = (Documnets) iter.next();
				if (tptd.getIdCatalogyDoc().equals("built")) {
					idType = mgBuilt.getBuiltsByID(tptd.getIdType()).getName();					
					tptd.setIdType(idType);
				} else if (tptd.getIdCatalogyDoc().equals("land")) {

					idType = mgLand.getLandByID(tptd.getIdType()).getName();
					
					tptd.setIdType(idType);
				}else{
					tptd.setIdType("");
				}

				listDoc[i] = tptd;
				i++;
			}
		}
		return listDoc;
	}

	public void editDocuments(Documnets doc) {
		mgDoc.editDocuments(doc);
	}

	public void deleteDoc(String id) {
		mgDoc.deleteDoc(id);
	}

	public Documnets getDocByID(String id) {
		return mgDoc.getDocByID(id);
	}

	public boolean deleteFile(String filePath, String fileName) {
		return mgDoc.deleteFile(filePath, fileName);
	}
	
	public void downloadFile(String fAddress, String localFileName,
			String destinationDir) {
		mgDoc.downloadFile(fAddress, localFileName, destinationDir);
	}

}
